package cn.gok.lambda;


import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @ClassName Stream操作续
 * @Description TODO
 * @Author HP
 * @Date 9:36 2022/7/12
 * @Version 1.0
 **/
class Book {
    private int bid;
    private String bname;
    private float price;

    public int getBid() {
        return bid;
    }

    public void setBid(int bid) {
        this.bid = bid;
    }

    public String getBname() {
        return bname;
    }

    public void setBname(String bname) {
        this.bname = bname;
    }

    public float getPrice() {
        return price;
    }

    public void setPrice(float price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Book{" +
                "bid=" + bid +
                ", bname='" + bname + '\'' +
                ", price=" + price +
                '}';
    }

    public Book(int bid, String bname, float price) {
        this.bid = bid;
        this.bname = bname;
        this.price = price;
    }
}

public class Stream操作续 {
    public static void main(String[] args) {
        //终止操作max();
        //终止操作collect();
        终止操作reduce();
    }

    private static void 终止操作reduce() {
        List<BigInteger> list = Arrays.asList(new BigInteger("10")
        ,new BigInteger("20")
                , new BigInteger("30")
                ,new BigInteger("40"));
        //BigInteger result = list.stream().reduce((new BigInteger("0")),(b1,b2)->{return b1.add(b2);});
        BigInteger result = list.stream().reduce(BigInteger.ZERO,BigInteger::add);
        BigInteger result1 = list.stream().reduce(BigInteger.ONE,BigInteger::multiply);
        System.out.println(result);
        System.out.println(result1);
    }

    private static void 终止操作collect() {
        List<Book> list = new ArrayList<>();
        Collections.addAll(list, new Book(1, "a", 45), new Book(2, "b", 95),
                new Book(3, "c", 42), new Book(4, "d", 400), new Book(5, "e", 60));
        List<Book> list1 = list.stream().filter(book -> book.getPrice() < 100f).collect(Collectors.toList());
        System.out.println(list1);

        Map<Integer,Book> map =
                list.stream().collect(Collectors.toMap(Book::getBid, bid -> bid, (bid1, bid2) -> bid1));
        System.out.println(map);

        //Map<String,List<Book>> map1=list.stream().collect(Collectors.groupingBy((book) -> book.getBname()));
        Map<String,List<Book>> map1=list.stream().collect(Collectors.groupingBy((Book::getBname)));
        
        System.out.println(map1);
    }

    private static void 终止操作max() {
        List<Book> list = new ArrayList<>();
        Collections.addAll(list, new Book(1, "a", 45), new Book(2, "b", 95),
                new Book(3, "c", 42), new Book(4, "d", 400), new Book(5, "e", 60));
        Stream<Book> stream = list.stream();

        Optional<Book> maxPriceBook = stream.max(new Comparator<Book>() {
            @Override
            public int compare(Book o1, Book o2) {
                return (int) (o1.getPrice() - o2.getPrice());
            }
        });
        if (maxPriceBook.isPresent()) {
            System.out.println("最大值存在");
        } else {
            System.out.println("最大值不存在");
        }
        Book max = maxPriceBook.get();
        System.out.println(max);
    }


}
